#!/usr/bin/env python

import wx
import app

class Presenter(object):
    '''Presenter class
    
    @author Julot <andy.julot@gmail.com>
    @since Saturday, January 14, 2012. 08:35 AM'''
    
    def __init__(self, view, interactor):
        self.view = view
        self.interactor = interactor
        self.interactor.install(self.view, self)
        self._readSize()
        self._readPos()
        self._readMaximized()
    
    def _readSize(self):
        config = app.Config()
        
        name = self.view.Name
        width = config.ReadInt('%s/Width' % name, -1)
        height = config.ReadInt('%s/Height' % name, -1)
        if width != -1 and height != -1:
            self.view.Size = wx.Size(width, height)
    
    def _readPos(self):
        config = app.Config()
        
        name = self.view.Name
        x = config.ReadInt('%s/X' % name, -1)
        y = config.ReadInt('%s/Y' % name, -1)
        if x == -1 or y == -1: self.view.CenterOnScreen()
        else: self.view.Position = wx.Point(x, y)
    
    def _readMaximized(self):
        config = app.Config()
        
        name = self.view.Name
        maximized = config.ReadInt('%s/Maximized' % name, 0)
        if maximized: self.view.Maximize()
    
    def _writeSize(self):
        if self.view.IsMaximized() or self.view.IsIconized(): return
        
        config = app.Config()
        
        name = self.view.Name
        config.WriteInt('%s/X' % name, self.view.Position.x)
        config.WriteInt('%s/Y' % name, self.view.Position.y)
    
    def _writePos(self):
        if self.view.IsMaximized() or self.view.IsIconized(): return
        
        config = app.Config()
        
        name = self.view.Name
        config.WriteInt('%s/Width' % name, self.view.Size.width)
        config.WriteInt('%s/Height' % name, self.view.Size.height)
    
    def _writeMaximized(self):
        config = app.Config()
        
        name = self.view.Name
        maximized = 1 if self.view.IsMaximized() else 0
        config.WriteInt('%s/Maximized' % name, maximized)
        
    def onActivate(self, active):
        '''Called when frame activated'''
        
        if active:
            self.view.SetTransparent(250)
        else:
            self.view.SetTransparent(250)
    
    def onClose(self):
        '''Called when frame closed'''
        
        self._writeSize()
        self._writePos()
        self._writeMaximized()
